package worker

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Config struct {
	EtcdEndPoints        []string `json:"etcdEndPoints"`
	EtcdUserName         string   `json:"etcdUserName"`
	EtcdPassword         string   `json:"etcdPassword"`
	EtcdDialTimeout      int      `json:"etcdDialTimeout"`
	MondbUrl             string   `json:"mondbUrl"`
	MondbConnectTimeOut  int      `json:"mondbConnectTimeOut"`
	JobLogDBName         string   `json:"jobLogDBName"`
	JobLogCollectionName string   `json:"jobLogCollectionName"`

	//批量插入到mongolog的定时时间
	JobLogCommitTimeOut int `json:"jobLogCommitTimeOut"`
	JobLogBatchSize     int `json:"jobLogBatchSize"`

	WorkerRegisterLeaseTTL int64 `json:"workerRegisterLeaseTTL"`
}

var (
	GlobalConfig *Config
)

func InitConfig(filename string) (err error) {
	var (
		//从文件中读取的字节数组
		content []byte
		//将字节数组反序列化的
		conf Config
	)

	//从文件中读配置文件
	if content, err = ioutil.ReadFile(filename); err != nil {
		return
	}

	fmt.Printf("%v", string(content))
	if err = json.Unmarshal(content, &conf); err != nil {
		return
	}
	//fmt.Printf("%+v", conf)
	GlobalConfig = &conf
	return
}
